<script setup lang="ts">
import { invoke } from "@tauri-apps/api/core";
import { useRouter } from 'vue-router';
import { load_ecommerce_data_analysis_service_dxc, load_xcom_service_dxc } from "./dxc";
import * as xcomService from "@/assets/api/xcomservice_0_0_1";
import * as userService from "@/assets/api/userservice_0_0_1";
import { reactive } from "vue";
const router = useRouter();
const data = reactive<{
  isInit: boolean
}>({
  isInit: false
});

// 让页面进来，就打开控制台，方便调试
invoke("open_devtools");

const connectToDataSourceNode = async () => {
  const res = await xcomService.BuildChannel({
    node_url: "dxm://127.0.0.1:9090",
    is_keep: true
  });

  console.log(res);
};


// 初始化 xPort
const onInitxPort = async () => {
  const is_init = await invoke("is_init_xport");
  if (!is_init) {
    // 启动 xport
    
    await invoke("boot_xport");
    await load_xcom_service_dxc();
    console.log("加载数据分析组件。。。")
    await load_ecommerce_data_analysis_service_dxc();
    await connectToDataSourceNode();
  }
  data.isInit = true
};

onInitxPort()

const isLogin = async () => {
  // 判断是否已经加载 xPort
  const is_init = await invoke("is_init_xport");
  if (!is_init) {
    // 如果未加载就跳转到登录页面
    router.push("/login");
    // 已经做好初始判断，可以显示页面
    data.isInit = true;
    return;
  }
  // 如果 xPort 已经加载，则判断已经登录，用于方式用户多次刷新页面，需要重登录
  const res = await userService.IsLogin();
  // 如果未登录，则跳转到登录页面
  if (res.data && !res.data.value) {
    router.push("/login");
  }
  data.isInit = true;
};
isLogin();
</script>

<template>
  <div v-if="data.isInit" id="app">
    <!-- 显示路由 -->
    <router-view />
  </div>
</template>

<style lang="scss">
#app {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}
</style>
